package com.yxr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxr.domain.ResponseResult;
import com.yxr.domain.entity.User;
import com.yxr.domain.exception.SystemException;
import com.yxr.domain.vo.UserInfoVo;
import com.yxr.enums.AppHttpCodeEnum;
import com.yxr.mapper.UserMapper;
import com.yxr.service.UserService;
import com.yxr.utils.BeanCopyUtils;
import com.yxr.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2024-07-08 09:15:37
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult userInfo() {
        //获取用户当前id
        Long userId = SecurityUtils.getUserId();
        //根据id查询用户信息
        User user = getById(userId);
        //封装成userInfoVo
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);

        return ResponseResult.okResult(userInfoVo);
    }

    @Override
    public ResponseResult updateUserInfo(User user) {

        updateById(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult register(User user) {
        //对数据进行非空判断 null 和" "
        if (!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        if (!StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        }
        if (!StringUtils.hasText(user.getEmail())){
            throw new SystemException(AppHttpCodeEnum.EMAIL_NOT_NULL);
        }
        if (!StringUtils.hasText(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_NOT_NULL);
        }
        //对数据判断是否存在
        if(userNameExit(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        if(nickNameNameExit(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIT);
        }
        //对密码加密
        String encode = passwordEncoder.encode(user.getPassword());
        //存入数据库
        user.setPassword(encode);
        save(user);
        return ResponseResult.okResult();
    }



    private boolean nickNameNameExit(String nickName) {
        LambdaQueryWrapper<User> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(User::getNickName,nickName);
        return count(QueryWrapper)>0;
    }

    private boolean userNameExit(String userName) {
        LambdaQueryWrapper<User> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(User::getUserName,userName);
       return count(QueryWrapper)>0;

    }
}
